//
//  BaseViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/3/28.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Foundation
import HandyJSON
import Alamofire
import CRRefresh

enum Direction : String {
    case left,right;
}

let kScreenWidth    = UIScreen.main.bounds.size.width
let kScreenHeight   = UIScreen.main.bounds.size.height
let pageSize = "10"

var bgWindow:DSHCBcakgroundWindow?
var page:Int?



func ScaleWidth(_ w: CGFloat) -> CGFloat {
    return w * kScreenWidth / 375.0
}

func ScaleHeight(have navi: Bool = true, _ h: CGFloat) -> CGFloat {
    if navi {
        return h * kScreenHeight / (667.0 - 64)
    }else{
        return h * kScreenHeight / (667.0)
    }
}

let limit = 10

class BaseViewController: UIViewController {

    var mainNavi: UINavigationController?
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = BaseColor.BackGroundColor
            self.navigationController?.isNavigationBarHidden = false
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        configNaiv()
    }
    func configNaiv() -> Void {
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:RGBColorWith(255, green: 255, blue: 255)]
    navigationController?.navigationBar.isTranslucent = false
    navigationController?.navigationBar.barTintColor = BaseColor.NavColor
    baseSetNavLeftButtonIsBack()
    
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

extension BaseViewController
{
    
    //MARK:设置SVProgressHUD样式
    
    func customShowInfo(_ content: String, time: TimeInterval = 1.0) -> Void {
        SVProgressHUD.setMinimumDismissTimeInterval(time)
        SVProgressHUD.setDefaultMaskType(.gradient)
        SVProgressHUD.showInfo(withStatus: content)
    }
    
    func customShowError(_ content: String, time: TimeInterval = 1.0) -> Void {
        SVProgressHUD.setMinimumDismissTimeInterval(time)
        SVProgressHUD.setDefaultMaskType(.gradient)
        SVProgressHUD.showError(withStatus: content)
    }
    
    func customShowSuccess(_ content: String, time: TimeInterval = 1.0) -> Void {
        SVProgressHUD.setMinimumDismissTimeInterval(time)
        SVProgressHUD.setDefaultMaskType(.gradient)
        SVProgressHUD.showSuccess(withStatus: content)
    }

    
    //设置左边item以及返回方式
    func baseSetNavLeftButtonIsBack()
    {
        baseSetNavButtonWith(nil, imageName: "backWhite", button: nil, direction: .left, tag: 100)
    }
    func baseSetNavLeftButtonIsDismiss()
    {
        baseSetNavButtonWith(nil, imageName: "backWhite", button: nil, direction: .left, tag: 200)
    }
    //左边按钮点击方法
    @objc func baseNavLeftButtonPressed(_ button : UIButton)
    {
        switch button.tag
        {
        case 100:
            _ = navigationController?.popViewController(animated: true)
        case 200:
            _ = dismiss(animated: true, completion: nil)
        default: break
        }
    }
    // 设置左边
    func baseSetNavLeftButtonWithTitle(_ title : String) {
        baseSetNavButtonWith(title, imageName: nil, button: nil, direction: .left, tag : 0)
    }
    func baseSetNavLeftButtonWithImage(_ imageName :String) {
        baseSetNavButtonWith(nil, imageName: imageName, button: nil, direction: .left, tag : 0)
    }
    func baseSetNavLeftButtonWithButton(_ button : UIButton) {
        baseSetNavButtonWith(nil, imageName: nil, button: button, direction: .left, tag : 0)
    }
    
    // 设置右边
    func baseSetNavRightButtonWithTitle(_ title : String) {
        self.baseSetNavButtonWith(title, imageName: nil, button: nil, direction: .right, tag: 0)
    }
    func baseSetNavRightButtonWithImage(_ imageName : String) {
        self.baseSetNavButtonWith(nil, imageName: imageName, button: nil, direction: .right, tag : 0)
    }
    func baseSetNavRightButtonWithButton(_ button : UIButton) {
        self.baseSetNavButtonWith(nil, imageName: nil, button: button, direction: .right, tag : 0)
    }
    //右边按钮点击方法
    @objc func baseNavRightButtonPressed(_ button : UIButton){
        
    }
    
    // 默认设置导航栏左右按钮
    fileprivate func baseSetNavButtonWith(_ title : String?, imageName : String?, button : UIButton?, direction : Direction, tag : Int){
        
        var btn : UIButton! = UIButton(type: .custom)
        var action : Selector? = nil
        switch direction{
        case .left :
            action = #selector(self.baseNavLeftButtonPressed(_:))
        case .right :
            action = #selector(self.baseNavRightButtonPressed(_:))
        }
        if title != nil {
            btn.setTitle(title, for: UIControlState())
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            btn.setTitleColor(UIColor.white, for: UIControlState())
            btn.addTarget(self, action: action!, for: .touchUpInside)
        }
        if imageName != nil {
            btn.setImage(UIImage(named: imageName!), for: UIControlState())
            btn.addTarget(self, action: action!, for: .touchUpInside)
        }
        if button != nil {
            btn = button
        }
        btn.tag = tag
        btn.sizeToFit()
        let barItem = UIBarButtonItem(customView: btn)
        let spaceItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target:nil, action: nil)
        
        switch direction {
        case .left :
            navigationItem.leftBarButtonItems = [spaceItem,barItem]
        case .right:
            navigationItem.rightBarButtonItems = [barItem,spaceItem]
        }
    }
    
    func replaceStrForPhone(str:String) -> String {
        
        if str.count < 8 {
            return "****"
        }else {
            let startIndex = str.index(str.startIndex, offsetBy: 3)
            let endIndex = str.index(str.startIndex, offsetBy: 6)
            let phone = str.replacingCharacters(in: startIndex...endIndex, with: "****")
            return phone
        }
    }
    
    
}


